package com.cn.bulu.service.impl;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.cn.bulu.constant.PayMethod;
import com.cn.bulu.dao.MerchantsWalletRecordDao;
import com.cn.bulu.pojo.MerchantsWalletRecord;
import com.cn.bulu.service.MerchantsWalletRecordService;
import com.cn.bulu.utils.DateUtils;
import com.cn.bulu.utils.LuKeOrderNoUtil;

@Service("merchantsWalletRecordService")
public class MerchantsWalletRecordServiceImpl implements MerchantsWalletRecordService {
	
	@Resource
	private MerchantsWalletRecordDao merchantsWalletRecordDao;
	
	

	//添加钱包记录,接收各种参数
	public int insertMerchantsWalletRecord(Integer merchantsId, Integer indentId, Integer merchantsType,
			Double mainMoney, Double addMoney, Double subMoney, Double finalGetMoney, Double finalLostMoney,
			Double walletRecharge, Double walletWithdrawals, Integer type, 
			Integer status, String remark,String alipayNum, Integer isDelete, String method) {
		//构造参数
		MerchantsWalletRecord merchantsWalletRecord = getMerchantsWalletRecord(merchantsId, indentId, merchantsType, 
				mainMoney, addMoney, subMoney, finalGetMoney, finalLostMoney, walletRecharge, walletWithdrawals, type, 
				 status, remark,alipayNum, isDelete, method);
		//插入数据库
		int result = merchantsWalletRecordDao.insertMerchantsWalletRecord(merchantsWalletRecord); 
		//返回主键
		return result>0?merchantsWalletRecord.getMerchantsWalletRecordId():0;
	}
	
	//查询单条记录
	public MerchantsWalletRecord selectMerchantsWalletRecordById(Map<String,Object> param){
		return merchantsWalletRecordDao.selectMerchantsWalletRecordById(param);
	}
	
	//插入到数据库
	public int insertMerchantsWalletRecord(MerchantsWalletRecord merchantsWalletRecord){
		//插入数据库
		int result = merchantsWalletRecordDao.insertMerchantsWalletRecord(merchantsWalletRecord); 
		//返回主键
		return result>0?merchantsWalletRecord.getMerchantsWalletRecordId():0;
	}
	
	
	
	
	
	//封装参数
	private MerchantsWalletRecord getMerchantsWalletRecord(Integer merchantsId, Integer indentId, Integer merchantsType,
			Double mainMoney, Double addMoney, Double subMoney, Double finalGetMoney, Double finalLostMoney,
			Double walletRecharge, Double walletWithdrawals, Integer type, Integer status, String remark,String alipayNum, 
			Integer isDelete, String method){
		//参数准备
		MerchantsWalletRecord merchantsWalletRecord = new MerchantsWalletRecord();
		//属性赋值
		merchantsWalletRecord.setMerchantsId(merchantsId);// merchantsId 商家或者个人id
		merchantsWalletRecord.setIndentId(indentId==null?null:indentId);// indentId 交易牵涉的订单id,充值提现的时候该值为0
		merchantsWalletRecord.setMerchantsType(merchantsType);// merchantsType 确认是商家还是个人
		merchantsWalletRecord.setMainMoney(mainMoney==null?0.00:mainMoney);// mainMoney 本次交易金额主干
		merchantsWalletRecord.setAddMoney(addMoney==null?0.00:addMoney);// addMoney 商家或者个人对订单进行加价悬赏
		merchantsWalletRecord.setSubMoney(subMoney==null?0.00:subMoney); // subMoney 商家或者个人违约扣款
		merchantsWalletRecord.setFinalGetMoney(finalGetMoney==null?0.00:finalGetMoney);// finalGetMoney 本次交易最终得到钱数
		merchantsWalletRecord.setFinalLostMoney(finalLostMoney==null?0.00:finalLostMoney);// finalLostMoney 本次交易最终失去金额
		merchantsWalletRecord.setWalletRecharge(walletRecharge==null?0.00:walletRecharge); // walletRecharge 充值金额
		merchantsWalletRecord.setWalletWithdrawals(walletWithdrawals==null?0.00:walletWithdrawals);// walletWithdrawals提现
		merchantsWalletRecord.setType(type);// type  交易类型
		merchantsWalletRecord.setRecordEndTime(status==1?DateUtils.getDateTime():"");// recordEndTime交易结束时间
		merchantsWalletRecord.setRecordBeginTime(DateUtils.getDateTime()); // recordBeginTime交易开始时间
		merchantsWalletRecord.setStatus(status==1?1:0);// status
		merchantsWalletRecord.setRemark(remark);// remark交易备注
		merchantsWalletRecord.setIsDelete(isDelete==1?1:0);// isDelete
		merchantsWalletRecord.setMethod(method);// method
		merchantsWalletRecord.setOrderNo(LuKeOrderNoUtil.getMerchantsOrderNo(merchantsId,PayMethod.getMethodIndex(method),type));// 商家订单号
		merchantsWalletRecord.setAlipayNum(alipayNum==null?"":alipayNum);//支付宝号码
		
		return merchantsWalletRecord;
	}
	
	
	

}
